package web

import (
	"Redbook/redbook/internal/Domain"
	"Redbook/redbook/internal/server"
	"github.com/gin-gonic/gin"
	"net/http"
)

type Logincontrol struct {
	Loginserver server.LoginServer
}

func (l *Logincontrol) RegistRouter(server *gin.Engine) {
	logist := server.Group("/")
	logist.Any("/sucess", l.Sucess)
	logist.Any("/error", l.Error)
	logist.GET("/login", l.Login)
	logist.POST("/login", l.Login)
}

func (l *Logincontrol) Login(c *gin.Context) {
	c.HTML(http.StatusOK, "login.gohtml", nil)
}
func (l *Logincontrol) Savelogin(c *gin.Context) {
	var user Man
	c.Bind(&user)
	err := l.Loginserver.Login(c.Request.Context(), Domain.User{
		ID:     user.ID,
		Passwd: user.Passwd,
	})
	if err != nil {
		c.Redirect(http.StatusTemporaryRedirect, "/error")
	} else {
		c.Redirect(http.StatusTemporaryRedirect, "/sucess")
	}
}
func (l *Logincontrol) Error(c *gin.Context) {
	c.String(http.StatusBadRequest, "err")
}

func (l *Logincontrol) Sucess(c *gin.Context) {
	c.Redirect(http.StatusTemporaryRedirect, "/article/new")
}
